package com.bytedance.scenedemo.restore;

import com.bytedance.scene.Scene;
import com.bytedance.scene.ui.bottomnavigation.ResUtil;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.utility.ColorUtil;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.ComponentState;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ScrollView;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.StateElement;

public class SupportRestoreRootScene extends Scene {
    private static int mClickCount;
    private Checkbox cbCheck;
    private StackLayout checkView;
    private Text textview;

    public static int getClickCount() {
        return mClickCount;
    }

    public static void setClickCount(int mClickCount) {
        SupportRestoreRootScene.mClickCount = mClickCount;
    }

    @Override
    public Component onCreateView(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        return scatter.parse(ResourceTable.Layout_support_restore_root_scene,container,false);
    }

    @Override
    public void onViewCreated(Component layout, Intent intent) {
        super.onViewCreated(layout, intent);
        ScrollView svMain = (ScrollView) layout.findComponentById(ResourceTable.Id_sv_push_pop_basic_usage_main);
        textview = (Text) layout.findComponentById(ResourceTable.Id_textview);
        int value=0;
        if(getArguments()!=null){
            value= (int) getArguments().getParam("value");
        }
        svMain.setBackground(ColorUtil.getMaterialColor(value));

        checkView = (StackLayout) layout.findComponentById(ResourceTable.Id_checkView);
        cbCheck = (Checkbox) layout.findComponentById(ResourceTable.Id_cb_check);
        StateElement stateElement = new StateElement();
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED},
                ResUtil.getPixelMapDrawable(getAbility(), ResourceTable.Media_checkbox_checked));
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY},
                ResUtil.getPixelMapDrawable(getAbility(), ResourceTable.Media_checkbox_normal));
        cbCheck.setButtonElement(stateElement);
    }

    @Override
    public void onSaveInstanceState(Intent intent) {
        super.onSaveInstanceState(intent);
        intent.setParam("mClickCount", mClickCount);
    }

    @Override
    public void onAbilityCreated(Intent intent) {
        super.onAbilityCreated(intent);
        String text = null;
        if (SupportRestoreAbility.getInState() != null) {
            text = "销毁后启动";
        } else {
            text = "正常启动";
        }
        showToast(text);
        if (SupportRestoreAbility.getInState() != null) {
            setClickCount(SupportRestoreAbility.getInState().getIntValue("mClickCount"));
            showToast("销毁后恢复成功！");
        }

        checkView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                if (cbCheck.isChecked()) {
                    cbCheck.setChecked(false);
                } else {
                    cbCheck.setChecked(true);
                }
                mClickCount++;
                showClickCount();
            }
        });
        showClickCount();
    }

    @Override
    public void onViewStateRestored(Intent intent) {
        super.onViewStateRestored(intent);
        showToast("销毁后恢复成功！");
        SupportRestoreAbility.setInState(null);
    }

    private void showClickCount() {
        textview.setText("总点击次数："+mClickCount);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
